package com.qqbot.utils;

import com.qqbot.enums.DatePattern;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static com.qqbot.enums.DatePattern.YYYY_MM_DD;


/**
 * 日期格式化工具类
 *
 * @author 付亚全
 */
public class DateFormatUtil {
    private static final int LENGTH_OF_TWO_CHARS = 2;

    /**
     * 格式化日期，规则为yyyy-MM-dd
     *
     * @param date 日期
     * @return 格式化后的日期字符串
     */
    public static String format(Date date) {
        return format(date, YYYY_MM_DD);
    }

    /**
     * 根据规则，格式化日期
     *
     * @param date    日期
     * @param pattern 规则
     * @return 格式化后的日期字符串
     */
    public static String format(Date date, DatePattern pattern) {
        if (null == date) {
            return "";
        }
        if (null == pattern) {
            pattern = YYYY_MM_DD;
        }
        if (DatePattern.YYYY_EE.equals(pattern)) {
            return DateUtil.readField(date, DateField.YEAR) + "-" + fillLength(DateUtil.readField(date, DateField.SEASON), LENGTH_OF_TWO_CHARS);
        }
        if (DatePattern.YYYY_EE_CN.equals(pattern)) {
            return DateUtil.readField(date, DateField.YEAR) + "年第" + fillLength(DateUtil.readField(date, DateField.SEASON), LENGTH_OF_TWO_CHARS) + "季度";
        }
        if (DatePattern.YYYY_FF.equals(pattern)) {
            return DateUtil.readField(date, DateField.YEAR) + "-" + fillLength(DateUtil.readField(date, DateField.HALF_YEAR), LENGTH_OF_TWO_CHARS);
        }
        if (DatePattern.YYYY_FF_CN.equals(pattern)) {
            return DateUtil.readField(date, DateField.YEAR) + "年" + (DateUtil.readField(date, DateField.HALF_YEAR) == 1 ? "上半年" : "下半年");
        }
        if (DatePattern.YYYY_WW.equals(pattern) || DatePattern.YYYY_WW_CN.equals(pattern)) {
            Calendar calendar = DateUtil.initCalendar(date);
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            date = calendar.getTime();
        }
        return buildDateFormatter(pattern).format(date);
    }

    /**
     * 将指定日期格式的日期字符串，转换成日期
     *
     * @param dateStr 日期字符串
     * @param pattern 规则
     * @return 日期
     */
    public static Date reformat(String dateStr, DatePattern pattern) {
        if (StringUtils.isBlank(dateStr)) {
            return null;
        }
        if (null == pattern) {
            pattern = YYYY_MM_DD;
        }
        if (DatePattern.YYYY_EE.equals(pattern)) {
            int year = Integer.parseInt(StringUtils.split(dateStr, "-")[0]);
            int season = Integer.parseInt(StringUtils.split(dateStr, "-")[1]);
            return DateUtil.buildDate(year, (season - 1) % DateUtil.MAX_VALUE_OF_SEASON * DateUtil.MONTH_WEIGHT_OF_SEASON + 1, 1);
        }
        if (DatePattern.YYYY_FF.equals(pattern)) {
            int year = Integer.parseInt(StringUtils.split(dateStr, "-")[0]);
            int halfYear = Integer.parseInt(StringUtils.split(dateStr, "-")[1]);
            return DateUtil.buildDate(year, (halfYear - 1) % DateUtil.MAX_VALUE_OF_HALF_YEAR * DateUtil.MONTH_WEIGHT_OF_HALF_YEAR + 1, 1);
        }
        if (DatePattern.YYYY_EE_CN.equals(pattern)) {
            int year = Integer.parseInt(StringUtils.split(dateStr, "年第")[0]);
            int season = Integer.parseInt(StringUtils.split(dateStr, "年第")[1].substring(0, 2));
            return DateUtil.buildDate(year, (season - 1) % DateUtil.MAX_VALUE_OF_SEASON * DateUtil.MONTH_WEIGHT_OF_SEASON + 1, 1);
        }
        if (DatePattern.YYYY_FF_CN.equals(pattern)) {
            int year = Integer.parseInt(StringUtils.split(dateStr, "年")[0]);
            int halfYear = StringUtils.split(dateStr, "年")[1].substring(0, 1).equals("上") ? 1 : 2;
            return DateUtil.buildDate(year, (halfYear - 1) % DateUtil.MAX_VALUE_OF_HALF_YEAR * DateUtil.MONTH_WEIGHT_OF_HALF_YEAR + 1, 1);
        }
        try {
            return buildDateFormatter(pattern).parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取SimpleDateFormat
     *
     * @param pattern 日期格式
     * @return SimpleDateFormat对象
     * @throws RuntimeException 异常：非法日期格式
     */
    private static SimpleDateFormat buildDateFormatter(DatePattern pattern) throws RuntimeException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern.getPattern());
        simpleDateFormat.setCalendar(DateUtil.initCalendar(new Date()));
        return simpleDateFormat;
    }

    /**
     * 补齐数字长度
     *
     * @param number 原始数字
     * @param length 固定长度
     * @return 补齐长度的字符串
     */
    private static String fillLength(int number, int length) {
        String numberStr = String.valueOf(number);
        while (numberStr.length() < length) {
            numberStr = "0" + numberStr;
        }
        return numberStr;
    }
}
